package com.sg.web;

import com.sg.bean.Food;
import com.sg.bean.Order;
import com.sg.bean.Shop;
import com.sg.bean.ShopCommodity;
import com.sg.service.FoodService;
import com.sg.service.OrderService;
import com.sg.service.ShopCommodityService;
import com.sg.service.ShopService;
import com.sg.vo.OrderVO;
import com.sg.vo.ShopVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author wangcf
 * @Description 购物车控制器
 * @date 2023/3/18 13:08
 */
@Controller
public class ShopController {

    Logger logger = Logger.getLogger(ShopController.class);

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopCommodityService shopCommodityService;

    @Autowired
    OrderService orderService;
    @Autowired
    FoodService foodService;
    /*@RequestMapping("shopcommoditylist")
    public String shopcommoditylist(int uId, int businessId, Model model) {
        logger.info("进入到购物车商品列表");
        List<ShopVO> list = shopService.findByUidAndBusinessId(uId, businessId);
        model.addAttribute("shopcommoditylist", list);
        return "user/shopcommoditylist";
    }*/

    @RequestMapping("shopadd")
    public void shopadd(int uId, int businessId, int comId) {
        logger.info("进入到添加购物车控制器");
        logger.info(comId);
        Shop shop = shopService.findByUidAndBusinessIdShop(uId, businessId);
        //如果没有这个购物车则创建一个
        logger.info(shop);
        if (shop == null) {
            logger.info("没有这个购物车创建一个");
            boolean i = shopService.add(new Shop(0, uId, 0, businessId));
            shop = shopService.findByUidAndBusinessIdShop(uId, businessId);
        }
        //有的话把商品添加进商品购物车关系表
        //查购物车商品关系存在不
        ShopCommodity shopCommodity = shopCommodityService.findByShopIdAndComId(shop.getShopId(), comId);
        logger.info(shopCommodity);
        if (shopCommodity == null) {
            logger.info("没有这个关系，创建一个");
            boolean i = shopCommodityService.add(new ShopCommodity(0, shop.getShopId(), comId, 1, 0));
            shopCommodity = shopCommodityService.findByShopIdAndComId(shop.getShopId(), comId);
            logger.info(i);
        } else {
            //有这个关系的话,给这个关系数量加一设置进去
            logger.info("有这个关系");
            shopCommodity.setCoNumber(shopCommodity.getCoNumber() + 1);
            boolean i = shopCommodityService.update(shopCommodity);
            logger.info(i);
        }

    }

    @RequestMapping("/shopcommoditylist")
    public String shopCommodityList(int uId,int businessId,Model model){
        logger.info(uId);
        List<ShopVO> list = shopService.findByUidAndBusinessId(uId,businessId);
        Shop shop = shopService.findByUidAndBusinessIdShop(uId,businessId);
        model.addAttribute("shoplist",list);
        model.addAttribute("shopprices",shop);


        return "shoplist";
    }
    @RequestMapping("balance")
    public String balace(Order order){
        orderService.add(order);
        return "redirect:toaddlocation.action";
    }
    @RequestMapping("/toaddlocation")
    public String toAddLoaction(){
        logger.info("进入添加地址页面");
        return "addlocation";
    }
    @RequestMapping("/addlocation")
    public String addLoaction(Food food){
        logger.info("添加地址页面");
        foodService.add(food);
        return "redirect:toindentlist.action";
    }
    @RequestMapping("/toindentlist")
    public String toIndentList(Model model){
        logger.info("订单页面");
        List<OrderVO> list =orderService.findAll();
        model.addAttribute("list",list);
        return "indentlist";
    }
}
